﻿using System.Collections.ObjectModel;
using System.Linq;
using Commands;
using ImageConverter.Model;
using ImageConverter.Model.Enums;

namespace ImageConverter.ViewModels
{
    public class CompleteViewModel : ViewModelBase, ICompleteViewModel
    {
        public CompleteViewModel()
        {
            CloseWindowCommand = new DelegateCommand<object>(OnCloseWindowCommand);
        }

        #region ICompleteViewModel Members

        public int SuccessItemsCount
        {
            get
            {
                return successItemsCount;
            }
            private set
            {
                successItemsCount = value;
                NotifyOfPropertyChange(() => SuccessItemsCount);
            }
        }

        public int WarningItemsCount
        {
            get
            {
                return warningItemsCount;
            }
            private set
            {
                warningItemsCount = value;
                NotifyOfPropertyChange(() => WarningItemsCount);
            }
        }

        public int ErrorItemsCount
        {
            get
            {
                return errorItemsCount;
            }
            private set
            {
                errorItemsCount = value;
                NotifyOfPropertyChange(() => ErrorItemsCount);
            }
        }

        public int TotalItemsCount
        {
            get
            {
                return totalItemsCount;
            }
            private set
            {
                totalItemsCount = value;
                NotifyOfPropertyChange(() => TotalItemsCount);
            }
        }

        public ObservableCollection<ContainerItem> Containers
        {
            get
            {
                return containers;
            }
            set
            {
                containers = value;
                NotifyOfPropertyChange(() => Containers);

                OnContainersChanged(value);
            }
        }

        public bool? DialogResult
        {
            get
            {
                return dialogResult;
            }
            private set
            {
                dialogResult = value;
                NotifyOfPropertyChange(() => DialogResult);
            }
        }

        public DelegateCommand<object> CloseWindowCommand { get; private set; }

        #endregion

        #region Private methods

        private void OnCloseWindowCommand(object parameter)
        {
            DialogResult = true;
        }

        private void OnContainersChanged(ObservableCollection<ContainerItem> containers)
        {
            if (containers == null)
                return;

            TotalItemsCount = containers.Count;
            SuccessItemsCount = containers.Where(c => c.Entries.All(e => e.Status == ItemStatusEnum.Success) 
                && c.Status != ItemStatusEnum.Error).Count();

            ErrorItemsCount = containers.
                Where(c => c.Entries.Any(e => e.Status == ItemStatusEnum.Error) || c.Status == ItemStatusEnum.Error).Count();

            WarningItemsCount = containers.
                Where(c => c.Entries.Any(e => e.Status == ItemStatusEnum.Warning) || c.Status == ItemStatusEnum.Warning).Count();
        }

        #endregion


        #region Private fields

        private int successItemsCount;
        private int warningItemsCount;
        private int errorItemsCount;
        private int totalItemsCount;
        private ObservableCollection<ContainerItem> containers;
        private bool? dialogResult;

        #endregion
    }
}
